﻿using System.Collections.Generic;
using System.Linq;
using Battle.Common;
using CardGameCore.Common;

namespace CardGameCore.Battle
{
    public class GamePrepare : IGamePrepare
    {
        private readonly IBattlefield _battlefield;
        private readonly IRandom _random;

        
        public GamePrepare(
            IBattlefield battlefield,
            IRandom random)
        {
            _battlefield = battlefield;
            _random = random;
        }

        public void Process()
        {
            ProcessCardPool();
        }

        private void ProcessCardPool()
        {
            Shuffle(_battlefield.Player.CardPool);
            Shuffle(_battlefield.Enemy.CardPool);
        }

        private void Shuffle<T>(IList<T> list)  
        {  
            var n = list.Count;  
            while (n > 1) {  
                n--;  
                var k = _random.Next(n + 1);  
                var value = list[k];  
                list[k] = list[n];  
                list[n] = value;  
            }  
        }
    }
}